<template>
    <div class="comment">
        <div class="nav">
            <div class="container">
                <div class="back" @click="back"><van-icon name="arrow-left" size="0.9rem" /></div>
                <h3 class="title"><span>{{ arr.length }}</span>条评论</h3>
            </div>
        </div>
        <div class="main">
            <div class="container">
                <div class="num">
                    <span>{{ arr.length }}</span>
                    <span>条短评</span>
                </div>
                <div class="list">
                    <ul>
                        <li v-for="(item, index) in arr">
                            <div class="icon">
                                <img :src=item.img alt="">
                            </div>
                            <div class="name">
                                <span>{{ item.author }}</span>
                            </div>
                            <div class="context">
                                <span>{{ item.content }}</span>
                            </div>
                            <div class="time">
                                <span>{{ item.time }}</span>
                            </div>
                            <div class="good">
                                <van-icon name="good-job-o" size="0.7rem" />
                            </div>
                            <div class="say">
                                <van-icon name="smile-comment-o" size="0.7rem" />
                            </div>
                            <div class="genduo">
                                <van-icon name="ellipsis" />
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="buttom"><span>{{ buttom }}</span></div>
        <div style="width: 100px;height: 60px;"></div>
        <div class="fabu">
            <div class="container">
                <div class="toux">
                    <img src="https://www.keaitupian.cn/cjpic/frombd/1/253/1215285637/1396751085.jpg" alt="">
                </div>
                <div class="inp">
                    <input type="text" @keydown="tap" v-model="text" placeholder="说说你的看法~">
                </div>
            </div>
        </div>

    </div>
</template>
<style lang="less" scoped>
.comment {
    width: 100%;
    position: absolute;

    .container {
        width: 94%;
        margin: 0 auto;
        position: relative;
    }

    .nav {
        width: 100%;
        height: 50px;
        background-color: #fff;
        position: fixed;
        overflow: hidden;
        z-index: 1000;

        .back {
            position: absolute;
            top: 12px;
            left: 0;
        }

        .title {
            width: 140px;
            position: absolute;
            top: 10px;
            left: calc(50% - 50px);
            font-size: 0.65rem;
        }
    }

    .main {
        margin-top: 50px;
        width: 100%;

        .num {
            margin-top: 20px;
            font-size: 20px;

            font-weight: 500;
        }

        .list {
            margin-top: 10px;
            width: 100%;

            ul {
                width: 100%;

                li {
                    width: 100%;
                    // height: 80px;
                    position: relative;
                    border-bottom: 1px solid #ccc;
                    overflow: hidden;

                    .icon {
                        width: 14%;
                        position: absolute;
                        top: 10px;
                        left: 0;

                        img {
                            width: 100%;
                            border-radius: 50%;
                        }
                    }

                    .name {
                        position: absolute;
                        top: 10px;
                        left: 60px;
                        font-size: 18px;
                        font-weight: 600;
                    }

                    .context {
                        margin-top: 40px;
                        margin-left: 60px;
                        font-size: 18px;
                    }

                    .time {
                        margin-top: 20px;
                        font-size: 16px;
                        margin-left: 60px;
                        color: #cfcfcf;
                        margin-bottom: 20px;
                    }

                    .good {
                        position: absolute;
                        right: 60px;
                        bottom: 15px;
                    }

                    .say {
                        position: absolute;
                        right: 0;
                        bottom: 15px;
                    }

                    .genduo {
                        position: absolute;
                        top: 10px;
                        right: 0;
                    }
                }
            }
        }
    }

    .fabu {
        width: 100%;
        background-color: #ebebeb;
        height: 60px;
        position: fixed;
        bottom: 0;
        left: 0;
        z-index: 1002;

        .toux {
            width: 12%;
            position: absolute;
            top: 5px;

            img {
                width: 100%;
                border-radius: 50%;
            }
        }

        .inp {
            position: absolute;
            top: 10px;
            left: 50px;

            input {
                font-size: 20px;
                background-color: #ebebeb;
            }
        }
    }

    .buttom {
        color: #bcbaba;
        width: 200px;
        height: 40px;
        margin-top: 20px;
        font-size: 20px;
        margin-left: calc(50% - 50px);
    }
}
</style>

<script>
import { comment } from '@/api/index'
import bus from '@/until/bus'
import { Dialog } from 'vant';

export default {
    data() {
        return {
            arr: [],
            cid: '',
            buttom: '',
            text: '',
        }
    },
    methods: {
        back() {
            this.$router.go(-1)
        },
        async getComment() {
            await comment()
                .then(content => {
                    console.log('-->', content);
                    let { comments } = content
                    console.log(comments);
                    let newArr = comments.map(item => {
                        let obj = {}
                        obj.author = item.author
                        obj.content = item.content

                        let time = item.time * 1000
                        let month = new Date(time).getMonth() + 1
                        month = month <= 10 ? '0' + month : '' + month
                        let date = new Date(time).getDate()
                        date = date <= 10 ? '0' + date : '' + date
                        let h = new Date(time).getHours()
                        h = h <= 10 ? '0' + h : '' + h
                        let m = new Date(time).getMinutes()
                        m = m <= 10 ? '0' + m : '' + m

                        obj.time = month + '-' + date + ' ' + h + ':' + m
                        obj.img = item.avatar
                        obj.id = item.id
                        obj.like = item.id
                        return obj
                    })
                    this.arr = newArr
                    if (this.arr.length > 0) {
                        this.buttom = '已经到底了~'
                    } else {
                        this.buttom = '还没有评论'
                    }
                })
        },
        tap(e) {
            if (e.code == 'Enter') {
                let obj = {}
                obj.content = this.text
                obj.author = '发布者'

                let month = new Date().getMonth() + 1
                month = month <= 10 ? '0' + month : '' + month
                let date = new Date().getDate()
                date = date <= 10 ? '0' + date : '' + date
                let h = new Date().getHours()
                h = h <= 10 ? '0' + h : '' + h
                let m = new Date().getMinutes()
                m = m <= 10 ? '0' + m : '' + m
                obj.time = month + '-' + date + ' ' + h + ':' + m
                obj.img = 'https://www.keaitupian.cn/cjpic/frombd/1/253/1215285637/1396751085.jpg'
                Dialog.confirm({
                    title: '提示',
                    message: '是否发布评论',
                }).then(() => {
                    this.text = ''
                    this.arr.unshift(obj)
                }).catch(() => {
                    return false
                })

            }

        }
    },
    created() {

    },
    mounted() {
        let query = this.$route.query;
        this.cid = query.rid;
        bus.$emit('sendCid', `${this.cid}`);
        this.getComment()
    },
}
</script>